package com.ruoyi.web.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.sendSms.SendSms;
import com.ruoyi.common.utils.sendSms.Sms;
import com.ruoyi.common.utils.uuid.IdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Api("生成验证码接口")
@RestController
@RequestMapping("/getSmd")
public class GenerateSms {
    @Autowired
    private RedisCache redisCache;

    private Logger logger = LoggerFactory.getLogger(getClass());


    @ApiOperation("注册验证码")
    @GetMapping(value = "/sms/code/{mobile}")
    public AjaxResult sms(@ApiParam(name="手机号码",required=true)@PathVariable("mobile") String mobile) throws Exception {
        // 保存验证码信息
        String uuid = IdUtils.simpleUUID();
        String verifyKey = CacheConstants.SMS_CAPTCHA_CODE_KEY + uuid;

        int code = (int) Math.ceil(Math.random() * 9000 + 1000);
        Map<String, Object> map = new HashMap<>(16);
        map.put("mobile", mobile);
        map.put("code", code);

        redisCache.setCacheObject(verifyKey, map, Constants.SMS_EXPIRATION, TimeUnit.MINUTES);
        //发送短信内容
        //短信推送
        //发送短信验证码
        Sms sms = new Sms();
        sms.setPhoneNumbers(mobile);
        sms.setSingnName("矿山安全检测云平台");
        sms.setTemplateCode("SMS_464655032");
        sms.setCode(String.valueOf(code));
        SendSms sendSms = new SendSms();
        sendSms.sendSms(sms);
        logger.info(" 为 {} 设置短信验证码：{}", mobile, code);
        return AjaxResult.success(uuid);
    }

    @ApiOperation("变更密码生成验证码")
    @GetMapping("/sms/smsChange/{mobile}")
    public AjaxResult smsChange(@ApiParam(name="手机号码",required=true)@PathVariable("mobile") String mobile) throws Exception {
        // 保存验证码信息
        String uuid = IdUtils.simpleUUID();
        String verifyKey = CacheConstants.SMS_CAPTCHA_CODE_KEY + uuid;
        int code = (int) Math.ceil(Math.random() * 9000 + 1000);
        Map<String, Object> map = new HashMap<>(16);
        map.put("mobile", mobile);
        map.put("code", code);
        redisCache.setCacheObject(verifyKey, map, Constants.SMS_EXPIRATION, TimeUnit.MINUTES);
        //发送短信内容
        //短信推送
        //发送短信验证码
        Sms sms = new Sms();
        sms.setPhoneNumbers(mobile);
        sms.setSingnName("矿山安全检测云平台");
        sms.setTemplateCode("SMS_464655032");
        sms.setCode(String.valueOf(code));
        SendSms sendSms = new SendSms();
        sendSms.sendSms(sms);
        logger.info(" 为 {} 设置短信验证码：{}", mobile, code);
        return  AjaxResult.success(uuid);
    }

        public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.dysmsapi20170525.Client client = SendSms.createClient();
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setSignName("矿山安全检测云平台");
        sendSmsRequest.setTemplateCode("SMS_464655032");
        HashMap<String, Object> paramMap = new HashMap<String, Object>(16);
        int code = (int) Math.ceil(Math.random() * 9000 + 1000);
        paramMap.put("code", code);
        JSONObject json = new JSONObject(paramMap);
        sendSmsRequest.setTemplateParam(JSONObject.toJSONString(json));
        sendSmsRequest.setPhoneNumbers("15605205636");
        SendSmsResponse response = client.sendSms(sendSmsRequest);
        System.out.println(response.getBody().getMessage());
    }

}
